﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;

namespace DDD.Extensions
{
    public static class ConfigurationExtension
    {
        public static IHostBuilder ReplaceIConfiguration(this IHostBuilder hostBuilder)
        {
            hostBuilder.ConfigureAppConfiguration((hostContext, config) =>
            {
                config = Configuration.ConfigurationBuilder;
            });

            return hostBuilder;
        }

        public sealed class Configuration
        {
            public static IConfigurationBuilder ConfigurationBuilder { get; }

            static Configuration()
            {
                ConfigurationBuilder = new ConfigurationBuilder()
                   .SetBasePath(Directory.GetCurrentDirectory())
                   .AddJsonFile("appsettings.json")
                   .AddEnvironmentVariables();
            }

            public static IConfiguration Instance()
            {
                return ConfigurationBuilder.Build();
            }
        }
    }
}
